package net.borlis.processor.impl;

import net.borlis.processor.api.object.AccountObject;
import net.borlis.processor.api.service.AccountService;
import net.borlis.processor.dao.entity.AccountEntity;
import net.borlis.processor.dao.entity.UserEntity;
import net.borlis.processor.dao.repository.AccountRepository;
import net.borlis.processor.dao.repository.UserRepository;
import net.borlis.processor.impl.object.AccountObjectImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * Created by borlis on 24.12.2014.
 */
@Service
@Qualifier("accountServiceImpl")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private UserRepository userRepository;


    @Override
    public AccountObject create(
            BigInteger userId, String name, String surname, String login, String password
    ) {
        UserEntity userEntity = userRepository.find(userId);
        AccountEntity accountEntity = new AccountEntity(userEntity, name, surname, login, password);
        return new AccountObjectImpl(accountRepository.create(accountEntity));
    }

    @Override
    public AccountObject read(BigInteger id) {
        return new AccountObjectImpl(accountRepository.find(id));
    }

    @Override
    public AccountObject update(BigInteger id, String name, String surname, String login) {
        AccountEntity accountEntity = accountRepository.find(id);
        accountEntity.setSurname(surname);
        accountEntity.setName(name);
        accountEntity.setLogin(login);
        return new AccountObjectImpl(accountRepository.update(accountEntity));
    }

    @Override
    public void delete(BigInteger id) {
        accountRepository.delete(id);
    }
}
